package com.example.xjtuse_pc.engine;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.example.xjtuse_pc.domain.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 2016-5-7.
 * 业务方法，提供手机中所有应用信息
 */

public class AppInfoProvider {
    /**
     * 获取所有安装的app信息
     * @param context 上下文
     * @return
     */
    public static List<AppInfo> getAppInfos(Context context){
        List<AppInfo> appInfos=new ArrayList<AppInfo>();
        //获取包管理器
        PackageManager  pm=context.getPackageManager();
        //所有设备上包的信息
        List<PackageInfo> PackageInfos= pm.getInstalledPackages(0);
        for(PackageInfo pi:PackageInfos){
            AppInfo ai=new AppInfo();
            String packagename=pi.packageName;
            Drawable icon=pi.applicationInfo.loadIcon(pm);
            String  name=pi.applicationInfo.loadLabel(pm).toString();
            int flage=pi.applicationInfo.flags;
            if((flage&ApplicationInfo.FLAG_SYSTEM)==0){
                ai.setUserApp(true);
            }else {
                ai.setUserApp(false);
            }
            if((flage&ApplicationInfo.FLAG_EXTERNAL_STORAGE)==0){
                ai.setIsRom(true);
            }else {
                ai.setIsRom(false);
            }

            ai.setIcon(icon);
            ai.setPicname(packagename);
            ai.setName(name);
            appInfos.add(ai);
        }

        return  appInfos;
    }
}
